﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Drawing;
using System.Threading;
using Google.Protobuf.WellKnownTypes;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 更新发货申请_备件订单类型
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CRMBJOrderTypeUController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string gz_con;
        public readonly string con_oa;
        public readonly string con_erp;


        public CRMBJOrderTypeUController(ILogger<CRMBJOrderTypeURequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            con_oa = _configuration.GetConnectionString("OA_DbConnection");
            con_erp = _configuration.GetConnectionString("ERP_CRMDbConnection");

            _logger = loggerFactory.CreateLogger<CRMBJOrderTypeUController>();

        }

        /// <summary>
        /// 更新发货申请_备件订单类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostUpdateBJOrderTYpe([FromBody] CRMBJOrderTypeURequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            CRMBJOrderTypeUResponseDto re = new CRMBJOrderTypeUResponseDto();


            if (strparm.requestid ==null|| strparm.requestid.Trim()=="")
            {

                strMsg = "requestid不能为空！";
            }

            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            try
            {
                if (strMsg == "")
                {
                    dic = new Dictionary<string, string>();
                    dic.Add("requestid", strparm.requestid);
                    string sqlstr = MethodHelp.GetTranSQL("TMS0189", dic, con);//查询组件报价申请运算_单据数据源_报价明细_OA
                    if (sqlstr != "")
                    {
                        DataTable dt_bmx = DB.MSSQuery(sqlstr, con_oa);
                        if (dt_bmx.Rows.Count>0)
                        {
                            string order_type = "";
                            string gl_order_type = "";
                            string gl_lx = "";
                            //备件-直销订单类型
                            if (dt_bmx.Rows[0]["ddlxbpbj"].ToString()!=""&& dt_bmx.Rows[0]["ddlxbpbj"].ToString()!=null)
                            {
                                dic = new Dictionary<string, string>();
                                dic.Add("transaction_type_id", dt_bmx.Rows[0]["ddlxbpbj"].ToString());
                                string sqlstr_zx = MethodHelp.GetTranSQL("TMS0192", dic, con);//查询组件报价申请运算_单据数据源_报价明细_OA
                                if (sqlstr_zx!="")
                                {
                                    DataTable dt_o = DB.OracleQuery(sqlstr_zx, con_erp);
                                    if (dt_o.Rows.Count>0)
                                    {
                                        order_type = dt_o.Rows[0]["TRANSACTION_TYPE"].ToString();
                                    }
                                }
                            }

                            //备件-关联交易订单类型
                            if (dt_bmx.Rows[0]["gljyddlxbpbj"].ToString() != "" && dt_bmx.Rows[0]["gljyddlxbpbj"].ToString() != null)
                            {
                                dic = new Dictionary<string, string>();
                                dic.Add("TRANSACTION_TYPE_ID", dt_bmx.Rows[0]["gljyddlxbpbj"].ToString());
                                string sqlstr_zx = MethodHelp.GetTranSQL("TMS0191", dic, con);//查询组件报价申请运算_单据数据源_报价明细_OA
                                if (sqlstr_zx != "")
                                {
                                    DataTable dt_o = DB.OracleQuery(sqlstr_zx, con_erp);
                                    if (dt_o.Rows.Count > 0)
                                    {
                                        gl_order_type = dt_o.Rows[0]["TRANSACTION_TYPE_NAME"].ToString();
                                    }
                                }
                            }

                            //备件-关联交易路线
                            if (dt_bmx.Rows[0]["gljylxbpbj"].ToString() != "" && dt_bmx.Rows[0]["gljylxbpbj"].ToString() != null)
                            {
                                dic = new Dictionary<string, string>();
                                dic.Add("BUSINESS_HEADER_ID", dt_bmx.Rows[0]["gljylxbpbj"].ToString());
                                string sqlstr_zx = MethodHelp.GetTranSQL("TMS0190", dic, con);//查询组件报价申请运算_单据数据源_报价明细_OA
                                if (sqlstr_zx != "")
                                {
                                    DataTable dt_o = DB.OracleQuery(sqlstr_zx, con_erp);
                                    if (dt_o.Rows.Count > 0)
                                    {
                                        //gl_lx = dt_o.Rows[0]["BUSINESS_NAME"].ToString();
                                        gl_lx = dt_bmx.Rows[0]["gljylxbpbj"].ToString();
                                    }
                                }
                            }
                            if (order_type!=""|| gl_order_type!=""|| gl_lx!="")
                            {
                                dic = new Dictionary<string, string>();
                                dic.Add("requestid", strparm.requestid);
                                dic.Add("order_type", order_type);
                                dic.Add("gl_order_type", gl_order_type);
                                dic.Add("gl_lx", gl_lx);
                                strMsg = MethodHelp.InsertIntoTable("TMS0193", dic, con);
                            }
                        }
                    }
                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
